package com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.R;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.base.BaseActivity;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.custom_view.GlideImageLoader;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.contract.IPersonalInformationContract;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.persenter.PersonalInformationPersemter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.FormatUtils;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.MyToastUtils;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.SharedPreferencesUtils;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.view.CropImageView;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import cn.qqtheme.framework.picker.DatePicker;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2018/8/15.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class PersonalInformationActivity extends BaseActivity implements IPersonalInformationContract.IPersonalInformationView ,View.OnClickListener {

    private String TAG = PersonalInformationActivity.class.getSimpleName();
    private RelativeLayout rl_person_image;
    private ImageView iv_head;
    private RelativeLayout rl_niname;
    private TextView tv_niname;
    private RelativeLayout rl_phone_number;
    private TextView tv_phone_number;
    private RelativeLayout rl_gender;
    private TextView tv_gender;
    private RelativeLayout rl_date_birth;
    private TextView tv_date_birth;
    private RelativeLayout rl_professional;
    private TextView tv_professional;
    ArrayList<ImageItem> images = null;

    private PersonalInformationPersemter mPersonalInformationPersemter = new PersonalInformationPersemter(this);
    private ImagePicker imagePicker;
    private String url;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 1){
                setNiName(return_change_name);
            }else if(msg.what == 2){
                if(!TextUtils.isEmpty(url)){
                    imagePicker.getImageLoader().displayImage( PersonalInformationActivity.this,url, iv_head, 160, 150);
                  //  Glide.with(PersonalInformationActivity.this).load(url).into(iv_head);
                }

            }
        }
    };
    private String return_change_name;

    @Override
    protected void userAgreedPermissions() {

    }

    @Override
    protected View AddLayoutView() {
        View view = View.inflate(this, R.layout.activity_personal_information,null);
        rl_person_image = view.findViewById(R.id.activity_personal_information_rl_person_image);
        iv_head = view.findViewById(R.id.activity_personal_information_iv_head);
        rl_niname = view.findViewById(R.id.activity_personal_information_rl_niname);
        tv_niname =(TextView) view.findViewById(R.id.activity_personal_information_tv_niname);
        rl_phone_number = view.findViewById(R.id.activity_personal_information_rl_phone_number);
        tv_phone_number = view.findViewById(R.id.activity_personal_information_tv_phone_number);
        rl_gender = view.findViewById(R.id.activity_personal_information_rl_gender);
        tv_gender = view.findViewById(R.id.activity_personal_information_tv_gender);
        rl_date_birth = view.findViewById(R.id.activity_personal_information_rl_date_birth);
        tv_date_birth = view.findViewById(R.id.activity_personal_information_tv_date_birth);
        rl_professional = view.findViewById(R.id.activity_personal_information_rl_professional);
        tv_professional = view.findViewById(R.id.activity_personal_information_tv_professional);
        setClickListener();
        return view;
    }

    private void setClickListener() {
        rl_person_image.setOnClickListener(this);
        rl_niname.setOnClickListener(this);
        rl_phone_number.setOnClickListener(this);
        rl_gender.setOnClickListener(this);
        rl_date_birth.setOnClickListener(this);
        rl_professional.setOnClickListener(this);
    }

    @Override
    protected void initChildViewDate() {
        mPersonalInformationPersemter.initTitle();
        // todo 加载个人信息
        mPersonalInformationPersemter.loadPersonalInfo(this);





    }

    @Override
    protected void onClickedTvRight() {
        Log.e(TAG, "onClick: 点击提交数据" );
        // todo 点击提交数据
        mPersonalInformationPersemter.submitDate(this);

    }

    @Override
    protected void onClickedIvRight() {

    }

    @Override
    protected void onClickedIvLeft() {
        finish();
    }

    @Override
    public String getNiName() {
        return tv_niname.getText().toString().trim();
    }

    @SuppressLint("LongLogTag")
    @Override
    public void setNiName(String NiName) {
        // todo 未显示
        tv_niname.setText(NiName);

    }

    @Override
    public String getPhoneNumber() {
        return tv_phone_number.getText().toString().trim();
    }

    @Override
    public void setPhoneNumber(String PhoneNumber) {
        tv_phone_number.setText(PhoneNumber);
    }

    @Override
    public String getGender() {
        return tv_gender.getText().toString().trim();
    }

    @Override
    public void setGender(String Gender) {
        tv_gender.setText(Gender);
    }

    @Override
    public String getDateBirth() {
        return tv_date_birth.getText().toString().trim();
    }

    @Override
    public void setDateBirth(String DateBirth) {
        tv_date_birth.setText(DateBirth);
    }

    @Override
    public String getProfessional() {
        return tv_professional.getText().toString().trim();
    }

    @Override
    public void setProfessional(String Professional) {
        tv_professional.setText(Professional);
    }

    @Override
    public void showTtile() {
        showTilteBg();
        showTitleContent();
        showTileIvLeft();
        showTilteRightTVContent();
        //设备管理(6)
        setTitleContent("个人信息", 0xffffffff);
        setTilteRightTVContent("保存",0xffffffff);
        setIvLeftIcon(R.mipmap.left_white_return);
        setTitleContentSize(16);
    }

    @Override
    public void showPersonImage() {
        Log.e(TAG, "showPersonImage: ImagePicker 对象" );
        // todo 获取的 ImagePicker 对象
        imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader()); //TODO 设置图片加载器
        imagePicker.setMultiMode(false);  //TODO 多选
        imagePicker.setShowCamera(true);  //TODO 显示相机 显示拍照按钮
        imagePicker.setSaveRectangle(false); //TODO 是否按矩形区域保存图片

        imagePicker.setStyle(CropImageView.Style.CIRCLE);
        Integer radius = Integer.valueOf(180);
        radius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radius,  getResources().getDisplayMetrics());
        imagePicker.setFocusWidth(radius * 2);
        imagePicker.setFocusHeight(radius * 2);
        // TODO 设置图片保存宽高 保存文件的宽高
        imagePicker.setOutPutX(800);
        imagePicker.setOutPutY(800);

        //TODO 开启相册
        Intent intent = new Intent(this, ImageGridActivity.class);
        intent.putExtra(ImageGridActivity.EXTRAS_IMAGES,images);
        startActivityForResult(intent, 1);
    }

    @Override
    public void toNiNamePager() {
        Intent intent = new Intent(this,ChangeMettingNameActivity.class);
        intent.putExtra("change_name_pager",3);
        startActivityForResult(intent,3);
    }

    @Override
    public String getStringUrl() {
        if(url != null){
            return url.toString().trim();
        }else{
            String  imageUrl = (String) SharedPreferencesUtils.getInstance(this).getDate("imageUrl","String");
            return imageUrl;
        }

    }



    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.activity_personal_information_rl_person_image:
                mPersonalInformationPersemter.OnclickedPersonImage();
                break;
            case R.id.activity_personal_information_rl_niname:
                mPersonalInformationPersemter.OnclickedNiName();
                break;
            case R.id.activity_personal_information_rl_phone_number:
                mPersonalInformationPersemter.OnclickedPhoneNumber();
                break;
            case R.id.activity_personal_information_rl_gender:
                mPersonalInformationPersemter.OnclickedGender(this);
                break;
            case R.id.activity_personal_information_rl_date_birth:
                mPersonalInformationPersemter.OnclickedDateBirth();
                break;
            case R.id.activity_personal_information_rl_professional:
                mPersonalInformationPersemter.OnclickedProfessional();
                break;
        }
    }
    @Override
    public void showDateBirth() {
        DatePicker picker = new DatePicker(this, DatePicker.YEAR_MONTH_DAY);
        picker.setRange(1990, 2015);//年份范围
        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                setDateBirth(year + "-" + month + "-" + day );
            }
        });
        picker.show();
    }

    @Override
    public void showToastMessage(String message) {
        MyToastUtils.ShortToaseShow(this,message);
    }

    @Override
    public void finshPager() {
        finish();
    }

    @Override
    public void showPresonInfo() {
        // tv_niname.setText("难道是给你");
        String  username = (String) SharedPreferencesUtils.getInstance(this).getDate("userName","String");
        String  realName = (String) SharedPreferencesUtils.getInstance(this).getDate("realName","String");

        String  imageUrl = (String) SharedPreferencesUtils.getInstance(this).getDate("imageUrl","String");
        int  sex = (Integer) SharedPreferencesUtils.getInstance(this).getDate("sex","Integer");
        Glide.with(this).load(imageUrl).placeholder(R.mipmap.menu_head_defalt).error(R.mipmap.menu_head_defalt).into(iv_head);
        if(sex == 0){
            tv_gender.setText("女");
        }else if(sex == 1){
            tv_gender.setText("男");
        }else if(sex == 2){
            tv_gender.setText("未选择");
        }
        tv_niname.setText(realName);
        tv_phone_number.setText(username);
    }


    @SuppressLint("LongLogTag")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == 1) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                url = images.get(0).path;
                Message msg =  Message.obtain();
                msg.what =2;
                handler.sendMessage(msg);
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }else if(resultCode == 3){
            return_change_name = data.getStringExtra("return_change_name");
          if(return_change_name != null && !TextUtils.isEmpty(return_change_name)){
              Message msg =  Message.obtain();
              msg.what =1;
              handler.sendMessage(msg);
          }
        }
        //super.onActivityResult(requestCode, resultCode, data);
    }
}
